<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RSA+AES 加解密</title>
    <script type="text/javascript" th:src="@{~/js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/jsencrypt.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{~/js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{~/js/security.js}"></script>
    <script type="text/javascript" th:src="@{~/js/random.js}"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        $("#bt").click(
            function() {
                // 获取16位随机数,当做aes秘钥key
                var randomStr = randomString(16);
                // aes加密
                var data = Encrypt(JSON.stringify({name:"好看"}),randomStr);
                // rsa加密
                var encrypt = new JSEncrypt();
                encrypt.setPublicKey($("#servicePublicKey").val());
                var encrypted = encrypt.encrypt(randomStr);
                // 创建json对象
                var json = {
                    "requestData": data,
                    "encrypted": encrypted
                };
               $.ajax({
                    type: "POST",
                    url:"/testEncrypt",
                    data: JSON.stringify(json),
                    dataType:'json',
                    contentType: "application/json",
                    success: function(result) {
                        alert("返回的数据对象:"+result);
                        // rsa解密,获取到aes秘钥
                        var decrypt = new JSEncrypt();
                        decrypt.setPrivateKey($('#clientPrivateKey').val());
                        var aesKey = decrypt.decrypt(result.encrypted);
                        // 用aes秘钥进行解密
                        var resultData = Decrypt(result.requestData,aesKey);
                        alert("解密的数据：" + resultData);
                    }
                });
            });
    });
</script>
<h2>服务端公钥：</h2>
<textarea id="servicePublicKey" rows="5" cols="45">MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ9s1qlOyv9qpuaTqauW6fUftzE50rVk3yVPZwv1aO1Ch/XSEz76xCwkyvqpaqceRXrPpdBmO5+ruJ+I8osOHo7L5GWEOcMOO+8izp9hXKBBrmRMD4Egpn00k9DhVIEKp/vyddZPS/doxB8onhN6poTJDLdFLFVEicMf52caN9GQIDAQAB
</textarea>
<h2>客户端私钥：</h2>
<textarea id="clientPrivateKey" rows="5" cols="45">MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL2KSsvFr9yAtldC6AFAWF3EI9CYRn2P2Fp2trzUyoDGZGRzd4XBE4AeT/7f1+GYPCC2MT/Dhw7pcNpsv+aLlXfuKgHJLVCmi8XxmUmDP9GO9XdMRqe/GW6uJ1eU1N853jm4L35OZHYPNGTmnblTUXvjAXsrYpIa/aDKpm+23Gt/AgMBAAECgYBqtFMdWsKBkZiVkZ4JLk9RIl3DTibJA1UawKBpuCX0zzuvbW3JSAQRaX9BjoT7hPe8trUNH6eGFpeo7/Ys9UIEU61c33Q49NBPEVXH2+PhefIE74b8/H9cu1iYQyn9NSGKt0clo5/CU2G3OA7h+xqD9b7ifd4+DtdrS3KDpTPa0QJBAPhBkdpomvIWmUfI69WerwZHrzMZEFYHThG9SbWw0UgIjdKmSiQmrXawPZLe/o3BxPSIIXDzxiVAimdjVJcfC4kCQQDDc9nUvbLt4TryCfnR7x2KECh3BDEv/cuw/e67m0HYOdooV4KQ4aVous/TbbpcyPGcC180XCHjF11gqVa6hdTHAkBpEJcBsDOjMR093DKy/a1lIwFqxri7L+xCZbHES0jHC5e6BtZp5lSTXpMwjV997vvD4bkFbKX3LhFlIAy0yFbBAkAgvjC43gqypS+9yoQKcldtgKV2wsIGuyq7fN7YmPrf4Vk1tutNoC+YqusUDWbSEmu/a3xIhkK7C3f+MIAyASeTAkEA6CalrJUYBeDcaSHKhmJIceQ7baf8Q7uMYY5gRYuQ4rGudcXXaLi+o2I9f9in5qxE/SM/y9LuBmkzrCuy0tlNXA==
</textarea>
    <input type="button" id="bt" value="提交" />
</body>
</html>